home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / CompoundBorder.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  162 lines

  1. /*
  2.  * @(#)CompoundBorder.java    1.9 98/02/02
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20. package com.sun.java.swing.border;
  21.  
  22. import java.awt.Graphics;
  23. import java.awt.Insets;
  24. import java.awt.Component;
  25.  
  26. /**
  27.  * A composite Border class used to compose two Border objects
  28.  * into a single border by nesting an inside Border object within 
  29.  * the insets of an outside Border object.
  30.  *
  31.  * For example, this class may be used to add blank margin space
  32.  * to a component with an existing decorative border:
  33.  * <p>
  34.  * <code><pre>
  35.  *    Border border = comp.getBorder();
  36.  *    Border margin = new EmptyBorder(10,10,10,10);
  37.  *    comp.setBorder(new CompoundBorder(border, margin));
  38.  * </pre></code>
  39.  * <p>
  40.  * Warning: serialized objects of this class will not be compatible with
  41.  * future swing releases.  The current serialization support is appropriate
  42.  * for short term storage or RMI between Swing1.0 applications.  It will
  43.  * not be possible to load serialized Swing1.0 objects with future releases
  44.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  45.  * baseline for the serialized form of Swing objects.
  46.  *
  47.  * @version 1.9 02/02/98
  48.  * @author David Kloba
  49.  */
  50. public class CompoundBorder extends AbstractBorder {
  51.     protected Border outsideBorder;
  52.     protected Border insideBorder;
  53.  
  54.     /** 
  55.      * Creates a compound border with null outside and inside borders.
  56.      */
  57.     public CompoundBorder() {
  58.     this.outsideBorder = null;
  59.     this.insideBorder = null;
  60.     }        
  61.  
  62.     /** 
  63.      * Creates a compound border with the specified outside and 
  64.      * inside borders.  Either border may be null.
  65.      * @param outsideBorder the outside border
  66.      * @param insideBorder the inside border to be nested
  67.      */
  68.     public CompoundBorder(Border outsideBorder, Border insideBorder) {
  69.     this.outsideBorder = outsideBorder;
  70.     this.insideBorder = insideBorder;
  71.     }        
  72.  
  73.     /**
  74.      * Returns whether or not this compound border is opaque.
  75.      * Returns true if both the inside and outside borders are
  76.      * non-null and opaque; returns false otherwise.
  77.      */
  78.     public boolean isBorderOpaque() { 
  79.     return (outsideBorder != null && outsideBorder.isBorderOpaque()) &&
  80.                (insideBorder != null && insideBorder.isBorderOpaque()); 
  81.     }
  82.  
  83.     /** 
  84.      * Paints the compound border by painting the outside border
  85.      * with the specified position and size and then painting the
  86.      * inside border at the specified position and size offset by
  87.      * the insets of the outside border.
  88.      * @param c the component for which this border is being painted
  89.      * @param g the paint graphics
  90.      * @param x the x position of the painted border
  91.      * @param y the y position of the painted border
  92.      * @param width the width of the painted border
  93.      * @param height the height of the painted border
  94.      */
  95.     public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  96.     Insets  nextInsets;
  97.     int px, py, pw, ph;
  98.  
  99.     px = x;
  100.     py = y;
  101.     pw = width;
  102.     ph = height;
  103.  
  104.     if(outsideBorder != null) {
  105.         outsideBorder.paintBorder(c, g, px, py, pw, ph);        
  106.  
  107.         nextInsets = outsideBorder.getBorderInsets(c);
  108.         px += nextInsets.left;
  109.         py += nextInsets.top;
  110.         pw = pw - nextInsets.right - nextInsets.left;
  111.         ph = ph - nextInsets.bottom - nextInsets.top;
  112.     }
  113.     if(insideBorder != null) 
  114.           insideBorder.paintBorder(c, g, px, py, pw, ph);        
  115.  
  116.     }
  117.      
  118.     /**
  119.      * Returns the insets of the composite border by adding
  120.      * the insets of the outside border to the insets of the
  121.      * inside border.
  122.      * @param c the component for which this border insets value applies
  123.      */
  124.     public Insets getBorderInsets(Component c)       {
  125.     int top, left, bottom, right;
  126.     Insets  nextInsets;
  127.  
  128.     top = left = right = bottom = 0;
  129.     if(outsideBorder != null) {
  130.         nextInsets = outsideBorder.getBorderInsets(c);
  131.         top += nextInsets.top;
  132.         left += nextInsets.left;
  133.         right += nextInsets.right;
  134.         bottom += nextInsets.bottom;
  135.     }
  136.     if(insideBorder != null) {
  137.         nextInsets = insideBorder.getBorderInsets(c);
  138.         top += nextInsets.top;
  139.         left += nextInsets.left;
  140.         right += nextInsets.right;
  141.         bottom += nextInsets.bottom;
  142.     }
  143.  
  144.     return new Insets(top, left, bottom, right);
  145.     }
  146.  
  147.     /**
  148.      * Returns the outside border object.
  149.      */
  150.     public Border getOutsideBorder() {
  151.         return outsideBorder;
  152.     }
  153.  
  154.     /**
  155.      * Returns the inside border object.
  156.      */
  157.     public Border getInsideBorder() {
  158.         return insideBorder;
  159.     }
  160. }
  161.  
  162.